package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import com.utils.encrypt.MD5;

public class Util {
	public static String dateToString(Date date)
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	public static String dateToString(Date date,String style)
	{
		SimpleDateFormat sdf=new SimpleDateFormat(style);
		return sdf.format(date);
	}
	public  static Date stringToDate(String date,String style)
	{
		SimpleDateFormat sdf=new SimpleDateFormat(style);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
		}
		return new Date();
	}
	public static String md5Encrypt(String str)
	{
		return MD5.computeMD5(str);
	}
	public static String genAppNumber()
	{
		UUID uuid = UUID.randomUUID();
		String appNumber = "AP"+Util.md5Encrypt(uuid.toString().replace("-", "")).toUpperCase();
		return appNumber;
	}
	public static int daysOfTwo(String startdate,String enddate,String style){
		
		return daysOfTwo(stringToDate(startdate,style),stringToDate(enddate,style));
	}
	public static int daysOfTwo(Date startdate,Date enddate){
		Calendar aCalendar = Calendar.getInstance();
	    Calendar bCalendar = Calendar.getInstance();
	    
	    aCalendar.setTime(stringToDate(dateToString(startdate,"yyyy-MM-dd"),"yyyy-MM-dd"));
	    bCalendar.setTime(stringToDate(dateToString(enddate,"yyyy-MM-dd"),"yyyy-MM-dd"));
	    
	    int days = 0;
	    while(aCalendar.before(bCalendar)){
	         days++;
	         aCalendar.add(Calendar.DAY_OF_YEAR, 1);
	    }
	    return days;
	    
	    
	    /*
	     *  if (aCalendar.YEAR!=bCalendar.YEAR||aCalendar.MONTH!=bCalendar.MONTH||aCalendar.DATE!=bCalendar.DATE) {
	    	 
		     long ltime = lastdate.getTime() - firstdate.getTime();
		     
		     if (ltime < 24*60*60*1000) {
		    	 d = 1;
		     }
		     else 
		    	 d = (int) (ltime /24/60/60/1000);
		   //  System.out.println(ltime /24/60/60/1000);
	     }
	     * 
	     */
	    
	}
}
